package com.copera.cn.kaka;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.app.Application;
import android.app.TabActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.vo.PointPraiseSetVo;
import com.copera.cn.kaka.vo.ShopVo;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class ApplicationCustomer extends Application {
	public static final String CHARSET = HTTP.UTF_8;
	private static final String TAG_APP = "Tag_App";
	private String flag = null;
	private HttpClient httpClient;
	private String dept, category, fixname, fixquery, type;
	private int inspectCount, repairCount, borrowCount, givebackCount,
			allotCount, maintaincount, total;
	private String zmystp, bmystp,shopystp,ggystp;
	private Bitmap image, bmImage;
	private String zmjd, bmjd,shopjd,ggjd;
	private Bitmap personalPhoto;
	private File frontFile;
	private int takeType = 0;
	private String selectType;// 选择图片类型、正面、背面
	private ShopVo shop;
	private PointPraiseSetVo  setVo;//店面点赞设置
	
	private PointPraiseSetVo  shareSetVo;//店面分享设置
	private String enableCamera;//启动摄像头
	private Bitmap shopImage,ggimage;
	private String photoFile;//拍照临时文件，用于显示
	
	
	
	
	public String getPhotoFile() {
		return photoFile;
	}

	public void setPhotoFile(String photoFile) {
		this.photoFile = photoFile;
	}

	public String getGgjd() {
		return ggjd;
	}

	public void setGgjd(String ggjd) {
		this.ggjd = ggjd;
	}

	public Bitmap getGgimage() {
		return ggimage;
	}

	public void setGgimage(Bitmap ggimage) {
		this.ggimage = ggimage;
	}

	public String getGgystp() {
		return ggystp;
	}

	public void setGgystp(String ggystp) {
		this.ggystp = ggystp;
	}

	public String getShopjd() {
		return shopjd;
	}

	public void setShopjd(String shopjd) {
		this.shopjd = shopjd;
	}

	public String getShopystp() {
		return shopystp;
	}

	public void setShopystp(String shopystp) {
		this.shopystp = shopystp;
	}

	public Bitmap getShopImage() {
		return shopImage;
	}

	public void setShopImage(Bitmap shopImage) {
		this.shopImage = shopImage;
	}

	public String getEnableCamera() {
		return enableCamera;
	}

	public void setEnableCamera(String enableCamera) {
		this.enableCamera = enableCamera;
	}

	public PointPraiseSetVo getShareSetVo() {
		return shareSetVo;
	}

	public void setShareSetVo(PointPraiseSetVo shareSetVo) {
		this.shareSetVo = shareSetVo;
	}

	public PointPraiseSetVo getSetVo() {
		return setVo;
	}

	public void setSetVo(PointPraiseSetVo setVo) {
		this.setVo = setVo;
	}

	@SuppressWarnings("deprecation")
	public List<TabActivity> activityList = new ArrayList<TabActivity>();

	public void addActivity(@SuppressWarnings("deprecation") TabActivity activity) {
		activityList.add(activity);
	}

	public void close() {
		if(activityList.size()>0&&activityList.size()!=1){
			System.out.println("个数"+activityList.size());
			for(int i=0;i<activityList.size();i++){
				if(i!=activityList.size()-1){
					activityList.get(i).finish();
					activityList.remove(i);
				}
			}
			
			
		}
		
		
	}

	

	public void logout() {

		HttpUtils.clearCookie();
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getInspectCount() {
		return inspectCount;
	}

	public void setInspectCount(int inspectCount) {
		this.inspectCount = inspectCount;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public int getRepairCount() {
		return repairCount;
	}

	public void setRepairCount(int repairCount) {
		this.repairCount = repairCount;
	}

	public int getBorrowCount() {
		return borrowCount;
	}

	public void setBorrowCount(int borrowCount) {
		this.borrowCount = borrowCount;
	}

	public int getGivebackCount() {
		return givebackCount;
	}

	public void setGivebackCount(int givebackCount) {
		this.givebackCount = givebackCount;
	}

	public int getAllotCount() {
		return allotCount;
	}

	public void setAllotCount(int allotCount) {
		this.allotCount = allotCount;
	}

	public int getMaintaincount() {
		return maintaincount;
	}

	public void setMaintaincount(int maintaincount) {
		this.maintaincount = maintaincount;
	}

	public String getFixquery() {
		return fixquery;
	}

	public void setFixquery(String fixquery) {
		this.fixquery = fixquery;
	}

	private static ApplicationCustomer mInstance = null;

	public static ApplicationCustomer getInstance() {
		return mInstance;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getFixname() {
		return fixname;
	}

	public void setFixname(String fixname) {
		this.fixname = fixname;
	}

	@Override
	public void onCreate() {

		mInstance = this;
		initHttpClient();
		initImageLoader(getApplicationContext());
		Log.i(TAG_APP, "ApplicationCustomer create");
		super.onCreate();
	}

	private void initHttpClient() {
		Log.i(TAG_APP, "HttpClient create");
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, CHARSET);
		HttpProtocolParams.setUseExpectContinue(params, true);

		ConnPerRouteBean connPerRoute = new ConnPerRouteBean(400);
		ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

		ConnManagerParams.setTimeout(params, 60000);
		HttpConnectionParams.setConnectionTimeout(params, 60000);
		HttpConnectionParams.setSoTimeout(params, 4000);

		SchemeRegistry schReg = new SchemeRegistry();
		schReg.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		schReg.register(new Scheme("https",
				SSLSocketFactory.getSocketFactory(), 443));

		ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
				schReg);
		httpClient = new DefaultHttpClient(ccm, params);
		

	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public String getZmystp() {
		return zmystp;
	}

	public void setZmystp(String zmystp) {
		this.zmystp = zmystp;
	}

	public String getBmystp() {
		return bmystp;
	}

	public void setBmystp(String bmystp) {
		this.bmystp = bmystp;
	}

	public Bitmap getImage() {
		return image;
	}

	public void setImage(Bitmap image) {
		this.image = image;
	}

	public Bitmap getBmImage() {
		return bmImage;
	}

	public void setBmImage(Bitmap bmImage) {
		this.bmImage = bmImage;
	}

	public String getZmjd() {
		return zmjd;
	}

	public void setZmjd(String zmjd) {
		this.zmjd = zmjd;
	}

	public String getBmjd() {
		return bmjd;
	}

	public void setBmjd(String bmjd) {
		this.bmjd = bmjd;
	}

	public Bitmap getPersonalPhoto() {
		return personalPhoto;
	}

	public void setPersonalPhoto(Bitmap personalPhoto) {
		this.personalPhoto = personalPhoto;
	}

	public File getFrontFile() {
		return frontFile;
	}

	public void setFrontFile(File frontFile) {
		this.frontFile = frontFile;
	}

	public int getTakeType() {
		return takeType;
	}

	public void setTakeType(int takeType) {
		this.takeType = takeType;
	}

	public String getSelectType() {
		return selectType;
	}

	public void setSelectType(String selectType) {
		this.selectType = selectType;
	}

	public ShopVo getShop() {
		return shop;
	}

	public void setShop(ShopVo shop) {
		this.shop = shop;
	}
	/** 初始化ImageLoader */
	public static void initImageLoader(Context context) {
		File cacheDir = StorageUtils.getOwnCacheDirectory(context, "kaka/Cache");//获取到缓存的目录地址
		Log.d("cacheDir", cacheDir.getPath());
		//创建配置ImageLoader(所有的选项都是可选的,只使用那些你真的想定制)，这个可以设定在APPLACATION里面，设置为全局的配置参数
		ImageLoaderConfiguration config = new ImageLoaderConfiguration
				.Builder(context)
				//.memoryCacheExtraOptions(480, 800) // max width, max height，即保存的每个缓存文件的最大长宽
				//.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null) // Can slow ImageLoader, use it carefully (Better don't use it)设置缓存的详细信息，最好不要设置这个
				.threadPoolSize(3)//线程池内加载的数量
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				//.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation你可以通过自己的内存缓存实现
				//.memoryCacheSize(2 * 1024 * 1024)  
				///.discCacheSize(50 * 1024 * 1024)  
				.discCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
				//.discCacheFileNameGenerator(new HashCodeFileNameGenerator())//将保存的时候的URI名称用HASHCODE加密
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				//.discCacheFileCount(100) //缓存的File数量
				.discCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
				//.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				//.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);//全局初始化此配置
	}

}
